package com.example.myapplication.fragment;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;

import com.example.myapplication.R;
import com.example.myapplication.databinding.ActivityFragmentBinding;

public class FragmentActivity extends AppCompatActivity {

    private ActivityFragmentBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityFragmentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.fragment), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        getSupportFragmentManager().beginTransaction()
                .add(R.id.frame_layout, new FirstFragment())
                .addToBackStack(null)
                .commit();

        binding.firstBtn.setOnClickListener(v -> {
            FragmentManager manager = getSupportFragmentManager();
            FirstFragment firstFragment = new FirstFragment();
            Bundle bundle = new Bundle();
            bundle.putString("name", "张三");
            firstFragment.setArguments(bundle);
            manager.beginTransaction()
                    .replace(R.id.frame_layout, firstFragment)
                    .addToBackStack(null)
                    .commit();
        });
        binding.secondBtn.setOnClickListener(v -> {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_layout, new SecondFragment())
                    .addToBackStack(null)
                    .commit();
        });
    }

}